package xc.dao;

import xc.entity.Room;
import xc.utils.JdbcUtils;
import xc.utils.TransactionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RoomDao {


    public boolean existRoom(Room room) {
        String sql = "select * from ROOM where R_NAME = ?";
        try (Connection connection = JdbcUtils.getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, room.getrName());
            ResultSet rs = pstmt.executeQuery();
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean existRoom(String roomName) {
        String sql = "select * from ROOM where R_NAME = ?";
        try (Connection connection = JdbcUtils.getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, roomName);
            ResultSet rs = pstmt.executeQuery();
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }


    public void addRoom(Room room) throws SQLException {
        JdbcUtils jdbcUtils = new JdbcUtils();
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            TransactionManager.beginTransaction();
            String sql = "insert into ROOM(R_NAME,R_TYPE) values(?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, room.getrName());
            pstmt.setString(2, room.getrType());
            int rs = pstmt.executeUpdate();
            if (rs > 0) {
                System.out.println("添加成功！");
            } else {
                System.out.println("添加失败！");
                }
            TransactionManager.commit();
        } catch (Exception e) {
            try {
                TransactionManager.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }finally {
                jdbcUtils.closeResource(null, pstmt);
                TransactionManager.release();
            }
        }
    }

    public void deleteRoom(String roomName) throws SQLException {
        JdbcUtils jdbcUtils = new JdbcUtils();
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
                TransactionManager.beginTransaction();
                String sql = "delete from ROOM where R_NAME = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, roomName);
                int rs = pstmt.executeUpdate();
                if (rs > 0) {
                    System.out.println("删除成功！");
                } else {
                    System.out.println("删除失败！");
                }
                TransactionManager.commit();
        } catch (Exception e) {
            try {
                TransactionManager.rollback();
            }catch (SQLException ex){
                ex.printStackTrace();
            }finally {
                jdbcUtils.closeResource(null, pstmt);
                TransactionManager.release();
            }
        }
    }

    public void updateRoom(String roomName,String roomStatus) throws SQLException {
        JdbcUtils jdbcUtils = new JdbcUtils();
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            TransactionManager.beginTransaction();
            String sql = "update ROOM set R_NAME = ?,R_STATUS = ? where R_NAME = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, roomName);
            pstmt.setString(2, roomStatus);
            pstmt.setString(3, roomName);
            int rs = pstmt.executeUpdate();
            if (rs > 0) {
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }
            TransactionManager.commit();

        } catch (Exception e) {
            try {
                TransactionManager.rollback();
            }catch (SQLException ex){
                ex.printStackTrace();
            }
        }finally {
            jdbcUtils.closeResource(null, pstmt);
            TransactionManager.release();
        }
    }



    public void selectRoom() throws SQLException {
        JdbcUtils jdbcUtils = new JdbcUtils();
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            TransactionManager.beginTransaction();
            String sql = "select R_NAME,R_TYPE,R_STATUS from ROOM";
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
                String rName = rs.getString("R_NAME");
                String rType = rs.getString("R_TYPE");
                String rStatus = rs.getString("R_STATUS");
                System.out.println("R_NAME:"+rName+"  R_TYPE:"+rType+"  R_STATUS:"+rStatus);
            }
            TransactionManager.commit();
        }catch (Exception e){
            try {
                TransactionManager.rollback();
            }catch (SQLException ex){
                ex.printStackTrace();
            }
        }finally {
            jdbcUtils.closeResource(null, pstmt);
            TransactionManager.release();
        }
    }
}